package com.rongshu.view.penpal.adapter;

import android.content.Context;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.rongshu.R;
import com.rongshu.utils.GlideUtils;
import com.rongshu.view.penpal.bean.PenpalJoinVerifyResponse;
import com.rongshu.view.penpal.view.MemberVerifyAdapterListener;
import com.widget.CircleImageView;

import java.util.List;

/**
 * created by zhengweis on 2018/11/1.
 * description：
 */
public class VefityMemberAdapter extends RecyclerView.Adapter<VefityMemberAdapter.ViewHolder> {

    private Context context;
    private List<PenpalJoinVerifyResponse.ResponseBean> list;
    private MemberVerifyAdapterListener listener;

    public VefityMemberAdapter(Context context, List<PenpalJoinVerifyResponse.ResponseBean> list) {
        this.list = list;
        this.context = context;
    }

    public void setAdapter(MemberVerifyAdapterListener listener) {
        this.listener = listener;
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
        View view = LayoutInflater.from(context).inflate(R.layout.item_verify_member, viewGroup, false);
        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull final ViewHolder viewHolder, final int i) {
        GlideUtils.loadImageView(context, viewHolder.logo, list.get(i).getAvatar(), R.drawable.user_head_icon, R.drawable.user_head_icon);
        viewHolder.name.setText(list.get(i).getUsername());
        if (TextUtils.equals("await", list.get(i).getAuditState())) {
            viewHolder.refuse.setVisibility(View.VISIBLE);
            viewHolder.agree.setVisibility(View.VISIBLE);
            viewHolder.agree.setText("同意");
            viewHolder.refuse.setText("拒绝");
        } else if (TextUtils.equals("pass", list.get(i).getAuditState())) {
            viewHolder.refuse.setVisibility(View.GONE);
            viewHolder.agree.setVisibility(View.VISIBLE);
            viewHolder.agree.setText("已同意");
        } else if (TextUtils.equals("reject", list.get(i).getAuditState())) {
            viewHolder.refuse.setVisibility(View.VISIBLE);
            viewHolder.agree.setVisibility(View.GONE);
            viewHolder.refuse.setText("已拒绝");
        }
        viewHolder.agree.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (listener != null) {
                    listener.agree(i);
                }
            }
        });
        viewHolder.refuse.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (listener != null) {
                    listener.refuse(i);
                }
            }
        });
    }

    @Override
    public int getItemCount() {
        return list == null ? 0 : list.size();
    }

    public class ViewHolder extends RecyclerView.ViewHolder {
        private TextView name, refuse, agree;
        private CircleImageView logo;

        public ViewHolder(@NonNull View itemView) {
            super(itemView);
            logo = itemView.findViewById(R.id.author_logo);
            name = itemView.findViewById(R.id.author_name);
            refuse = itemView.findViewById(R.id.refuse);
            agree = itemView.findViewById(R.id.agree);
        }
    }
}
